//
//  ShopViewAvatarCell.swift
//  iModern
//
//  Created by Ralf on 7/29/15.
//  Copyright (c) 2015 com.starlooking. All rights reserved.
//

import UIKit

class ShopViewAvatarCell: TableViewCell, UIScrollViewDelegate
{

    let __preview_image_view_tag = 10
    let __favorites_button_tag   = 30
    let __scroll_view_tag        = 100
    let __pagecontrol_tag        = 101
    var images                   = [UIImageView]()
    let selfHeight: CGFloat      = 150
    var activity:   NSMutableDictionary!

    override func awakeFromNib()
    {
        super.awakeFromNib()

        //        initPreviewImageView()
        initScrollView()
        initPageControl()
        let imageCount  = 4
        let scrollView  = self.viewWithTag(__scroll_view_tag) as! UIScrollView
        let pageControl = self.viewWithTag(__pagecontrol_tag) as! UIPageControl

        let width = screen.size.width * CGFloat(imageCount)

        scrollView.contentSize = CGSizeMake(width, 0)
        //let imagesColor = [UIColor.blueColor(), UIColor.brownColor(), UIColor.redColor(), UIColor.orangeColor(), UIColor.cyanColor(), UIColor.grayColor()]
        for i in 0 ... 3
        {

            let x         = CGFloat(i) * screen.size.width
            let imageView = UIImageView()
            imageView.frame = CGRectMake(x, 0.0, screen.size.width, selfHeight)
            let imageName = "Shop-0\(i).jpg"
            imageView.image = UIImage(named: imageName)
            scrollView.addSubview(imageView)

        }

        pageControl.numberOfPages = imageCount
    }

    override func setSelected(selected: Bool, animated: Bool)
    {
        super.setSelected(selected, animated: animated)
    }

    func initScrollView()
    {
        var scrollView = self.viewWithTag(__scroll_view_tag) as? UIScrollView

        if scrollView == nil
        {
            let frame = CGRectMake(0, 0, screen.size.width, selfHeight)
            scrollView = UIScrollView(frame: frame)
            scrollView?.backgroundColor = UIColor.clearColor()
            scrollView!.pagingEnabled = true
            scrollView!.showsHorizontalScrollIndicator = false
            scrollView!.showsVerticalScrollIndicator = false
            scrollView!.scrollsToTop = false
            scrollView!.delegate = self
            scrollView!.tag = __scroll_view_tag

            self.addSubview(scrollView!)
        }
    }

    func initPageControl()
    {
        var pageControl = self.viewWithTag(__pagecontrol_tag) as? UIPageControl

        if pageControl == nil
        {
            pageControl = UIPageControl()

            pageControl!.frame = CGRectMake(0, selfHeight - 30, self.bounds.width, 20)
            pageControl!.pageIndicatorTintColor = UIColor.grayColor()
            pageControl!.backgroundColor = UIColor.clearColor()
            pageControl!.currentPageIndicatorTintColor = UIColor.whiteColor()
            pageControl!.tag = __pagecontrol_tag

            addSubview(pageControl!)
        }
    }

//    func addImageView(imageView: UIImageView)
//    {
//        images.append(imageView)
//        let scrollView  = self.viewWithTag(__scroll_view_tag) as! UIScrollView
//        let pageControl = self.viewWithTag(__pagecontrol_tag) as! UIPageControl
//        
//        let width = screen.size.width * CGFloat(images.count)
//        
//        scrollView.contentSize = CGSizeMake(width, 0)
//        
//        let x = CGFloat(images.count - 1) * screen.size.width
//        imageView.frame = CGRectMake(x, 0.0, screen.size.width, screen.size.width)
//        scrollView.addSubview(imageView)
//        
//        pageControl.numberOfPages = images.count
//        //        scrollView.contentOffset = CGPointMake(x, 0.0)
//    }

    func scrollViewDidScroll(scrollView: UIScrollView)
    {
        let pageControl = self.viewWithTag(__pagecontrol_tag) as! UIPageControl

        let x = CGFloat(scrollView.contentOffset.x)
        let y = CGFloat(scrollView.frame.size.width / 2)

        let page = Int((x + y) / scrollView.frame.size.width)

        pageControl.currentPage = page
    }

}
